﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Warranty.Model.Interface;
using Warranty.Model.Model;
using Common.Util;

namespace Warranty.Provider
{
    public class PndHistoryProvider : GenericProvider, IPndHistoryProvider
    {

        public bool UpdatePndHistory(SerialNumberPndHist obj)
        {
            try
            {
                var updateRecord = RetrievePndHistory(obj.SerialNo, obj.PndType);
                if (updateRecord == null) return false;
                updateRecord.Moseq = obj.Moseq;
                updateRecord.CfmDate = obj.CfmDate;
                updateRecord.CfmFlag = obj.CfmFlag;
                updateRecord.CfmType = obj.CfmType;
                updateRecord.CfmUser = obj.CfmUser;
                updateRecord.CmdCode = obj.CmdCode;
                updateRecord.EndUserCell = obj.EndUserCell;
                updateRecord.Model = obj.Model;
                updateRecord.MoMsgbody = obj.MoMsgbody;
                updateRecord.PndDays = obj.PndDays;
                updateRecord.PndType = obj.PndType;
                updateRecord.ReceiveDate = obj.ReceiveDate;
                updateRecord.RejectDate = obj.RejectDate;
                updateRecord.RejectFlag = obj.RejectFlag;
                updateRecord.RejectUser = obj.RejectUser;
                updateRecord.RespMsg = obj.RespMsg;
                updateRecord.RespType = obj.RespType;
                updateRecord.SerialNo = obj.SerialNo;
                updateRecord.ShopCell = obj.ShopCell;
                updateRecord.ShopCode = obj.ShopCode;
                updateRecord.SmsYn = obj.SmsYn; 
                updateRecord.SoTransferFlag = obj.SoTransferFlag;
                if (updateRecord.SoTransferFlag.IsYes()) updateRecord.SoTransferDate = DateTime.Now;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public SerialNumberPndHist RetrievePndHistory(string serial, string pndType)
        {
            var query = repository.GetQuery<SerialNumberPndHist>();
            if (!serial.IsNullOrWhiteSpace())
                query = query.Where(x => x.SerialNo.ToUpper() == serial.ToUpper() && x.PndType.ToUpper() == pndType.ToUpper());
            return query.FirstOrDefault();
        }

        public List<SerialNumberPndHist> SearchPndHistory(string serial, string pndType, bool? transferFlag)
        {
            var querry = repository.GetQuery<SerialNumberPndHist>();

            if (!serial.IsNullOrWhiteSpace())
                querry = querry.Where(x => x.SerialNo.ToUpper().Contains(serial.ToUpper()));

            if (!pndType.IsNullOrWhiteSpace())
                querry = querry.Where(x => x.PndType.ToUpper() == pndType.ToUpper());

            if (transferFlag.HasValue)
            {
                string flag = transferFlag.Value.ToBooleanString();
                if (!transferFlag.Value) querry = querry.Where(x => x.SoTransferFlag.ToUpper() == flag);
                else querry = querry.Where(x => x.SoTransferFlag == null || x.SoTransferFlag.Length == 0 || x.SoTransferFlag.ToUpper() == flag);
            }

            return querry.ToList();
        }
    }
}
